package top.szhome.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.szhome.develop.entity.Property;
import top.szhome.develop.service.PropertyService;
import top.szhome.framework.core.tools.support.Resp;
import top.szhome.framework.starter.db.support.SimpleServiceImpl;
import top.szhome.develop.convert.ModelConverter;
import top.szhome.develop.dto.ModelDTO;
import top.szhome.develop.entity.Model;
import top.szhome.develop.mapper.ModelMapper;
import top.szhome.develop.service.ModelService;
import top.szhome.develop.vo.ModelVO;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模型服务实现
 *
 * @author 唐加利
 * @date 2023/08/21
 **/
@AllArgsConstructor
@Service
@Slf4j
public class ModelServiceImpl extends SimpleServiceImpl<ModelMapper, ModelConverter, ModelDTO, Model, ModelVO> implements ModelService {

    private final PropertyService propertyService;

    @Override
    public List<Model> getByDatasource(Long datasourceId) {
        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(Model.class).eq(Model::getDatasourceId, datasourceId);
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> id) {
        id.stream().forEach(modelId -> {
            List<Property> properties = propertyService.getProperties(modelId);
            propertyService.delete(properties.stream().map(Property::getId).collect(Collectors.toList()));
        });
        return super.delete(id);
    }
}
